package net.hn.hnms.biz.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.project.domain.ApproveInfo;
import net.hn.hnms.biz.project.domain.StandingBookManage;
import net.hn.hnms.biz.project.mapper.ApproveInfoMapper;
import net.hn.hnms.biz.project.mapper.StandingBookManageMapper;
import net.hn.hnms.biz.project.service.ApproveInfoService;
import net.hn.hnms.biz.project.vo.ApproveInfoVo;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.utils.LoginHelper;
import net.hn.hnms.sys.system.api.RemoteUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApproveInfoServiceImpl implements ApproveInfoService {

    @Autowired
    private ApproveInfoMapper approveInfoMapper;

    @Autowired
    private StandingBookManageMapper standingBookManageMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Override
    public Page<ApproveInfoVo> getApproveInfo(PageQuery page, String username, String standingBookManageId) {

        if (StringUtils.isBlank(username)) username = LoginHelper.getLoginUser().getUsername();
        Page<ApproveInfo> ApproveInfoPage = approveInfoMapper.selectPage(page.build(), new LambdaQueryWrapper<ApproveInfo>()
                .eq(StringUtils.isNotEmpty(username)&&StringUtils.isBlank(standingBookManageId), ApproveInfo::getApproveUsername, username)
                .eq(StringUtils.isNotEmpty(standingBookManageId), ApproveInfo::getStandingBookManageId, standingBookManageId)
                .eq(ApproveInfo::getDataType, "0")
                .eq(StringUtils.isBlank(standingBookManageId),ApproveInfo::getApproveStatus, "0"));
        Page<ApproveInfoVo> ApproveInfoVoPage = new Page<>();

//        BeanCopyUtils.copy(ApproveInfoPage, ApproveInfoVoPage);

        List<ApproveInfoVo> voList = ApproveInfoPage.getRecords().stream().map(details -> {
            ApproveInfoVo vo = new ApproveInfoVo();
            BeanUtils.copyProperties(details, vo);
            StandingBookManage sbm = standingBookManageMapper.selectById(vo.getStandingBookManageId());
            vo.setProjectName(sbm.getEngineeringProject());
            vo.setBelongUnit(sbm.getBelongUnit());
            vo.setReportingLevel(sbm.getReportingLevel());

            return vo;
        }).collect(Collectors.toList());
        ApproveInfoVoPage.setRecords(voList);
        ApproveInfoVoPage.setTotal(voList.size());
        return ApproveInfoVoPage;

    }

    @Override
    public int approbe(String standingBookManageId, String isAgree,String opinion) {

        ApproveInfo flowInfo = getApproveInfo(standingBookManageId, "0");

        ApproveInfo insApproveInfo = new ApproveInfo();
        insApproveInfo.setOpinion(opinion);
        insApproveInfo.setApproveUsername(LoginHelper.getLoginUser().getUsername());
        insApproveInfo.setApproveNickname(LoginHelper.getLoginUser().getNickname());
        insApproveInfo.setDataType("1");
        insApproveInfo.setIsAgree(isAgree);
        insApproveInfo.setStandingBookManageId(flowInfo.getStandingBookManageId());
        String process = (Integer.parseInt(flowInfo.getApproveProcess()) + 1) + "";
        flowInfo.setApproveProcess(process);
        insApproveInfo.setApproveProcess(flowInfo.getApproveProcess());

        if ("0".equals(isAgree)) {
            StandingBookManage standingBookManage = getStandingBookManage(standingBookManageId);

            boolean isCoal = standingBookManage.getReportingLevel().equals("1") && flowInfo.getApproveProcess().equals("6");
            boolean isCompany = standingBookManage.getReportingLevel().equals("0") && flowInfo.getApproveProcess().equals("7");
            if (isCoal || isCompany) {
                //同意，并且是审批的最后一步，应该完结流程
                insApproveInfo.setApproveStatus("1");
                flowInfo.setApproveStatus("1");
                flowInfo.setApproveUsername("");
                flowInfo.setApproveNickname("");
            } else {
                flowInfo.setApproveUsername(getUsername(getStandingBookManage(standingBookManageId), process));
                flowInfo.setApproveNickname(remoteUserService.getUserInfo(flowInfo.getApproveUsername()).getNickname());
                insApproveInfo.setApproveProcess(process);
                insApproveInfo.setApproveStatus("0");
            }

        } else {
            //不同意
            flowInfo.setApproveUsername("");
            flowInfo.setApproveNickname("");
            flowInfo.setApproveStatus("2");
            insApproveInfo.setApproveStatus("2");
        }
        approveInfoMapper.updateById(flowInfo);
        return approveInfoMapper.insert(insApproveInfo);
    }

    public ApproveInfo getApproveInfo(String standingBookManageId, String dataType) {

        LambdaQueryWrapper<ApproveInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApproveInfo::getStandingBookManageId, standingBookManageId).eq(ApproveInfo::getDataType, dataType);
        ApproveInfo approveInfo = approveInfoMapper.selectOne(wrapper);

        return approveInfo;
    }

    public StandingBookManage getStandingBookManage(String standingBookManageId) {
        LambdaQueryWrapper<StandingBookManage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StandingBookManage::getId, standingBookManageId);
        StandingBookManage standingBookManage = standingBookManageMapper.selectOne(wrapper);


        return standingBookManage;
    }

    public String getUsername(StandingBookManage standingBookManage, String process) {
        String username = "";

        switch (process) {
            case "0":
                username = standingBookManage.getBusinessSupervisor();
                break;
            case "1":
                username = standingBookManage.getDispatchingRoom();
                break;
            case "2":
                username = standingBookManage.getSafetyInspection();
                break;
            case "3":
                username = standingBookManage.getVicePresident();
                break;
            case "4":
                username = standingBookManage.getViceCoalLeader();
                break;
            case "5":
                username = standingBookManage.getRatifyCoalLeader();
                break;
            case "6":
                username = standingBookManage.getCompanyDispatchingRoom();
                break;
            default:
                username = "admin";
        }

        return username;
    }
}
